/**
 * 
 */
package org.xlab.baseframework.conf;

import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.xlab.baseframework.job.WSSessionCleaningJob;

/**
 * @author MaKuBEX
 * 
 */
@Configuration
public class TimerConfiguration {
	@Bean(autowire = Autowire.BY_NAME, name = "bpf_sessionCleaningScheduler")
	public FactoryBean<JobDetail> sessionCleaningScheduler() {
		MethodInvokingJobDetailFactoryBean scheduler = new MethodInvokingJobDetailFactoryBean();
		scheduler.setTargetObject(wSSessionCleaningJob());
		scheduler.setTargetMethod("clean");
		return scheduler;
	}

	@Bean(autowire = Autowire.BY_NAME, name = "bpf_sessionCleaningScheduler_mgr")
	public SchedulerFactoryBean sessionCleaningSchedulerMgr()
			throws Exception {
		SchedulerFactoryBean manager = new SchedulerFactoryBean();
		manager.setTriggers(new Trigger[] { sessionCleaningSchedulerTiming()});
		return manager;
	}

	@Bean(autowire = Autowire.BY_NAME, name = "bpf_sessionCleaningScheduler_timing")
	public Trigger sessionCleaningSchedulerTiming()
			throws Exception {
		CronTriggerBean timing = new CronTriggerBean();
		timing.setJobDetail(sessionCleaningScheduler().getObject());
		timing.setCronExpression("0 */1 * * * ?");
		return timing;
	}

	@Bean(autowire = Autowire.BY_NAME, name = "bpf_sessionCleaningJob")
	public WSSessionCleaningJob wSSessionCleaningJob() {
		return new WSSessionCleaningJob();
	}
}
